-- wzip support for wTest
-- The MIT License (MIT)
-- Copyright (c) 2016 Yanghe Sun

fs    = require("path.fs")
miniz = require("miniz")
path  = require("path")
require "extract_all"

import join from path

class wzip
    new: (src, dest, cache = true) =>
        @_dest = dest
        @_src = src
        @_cached = false
        @_Extract! if cache

    Cache: => @_Extract! if not @_cached

    _Gc: => fs.removedirs(self._dest)
    _Join: (rpath) => join(join(self._dest, "data"), rpath)

    _PraseWdat: (wdat_path) =>
        conf = dofile(wdat_path)
        @max_cpu_time = conf.max_cpu_time
        @max_memory = conf.max_memory
        @datas = conf.datas
        @judge_func = conf.judge_func
        @score = 0
        for v in *@datas
            v[1] = @_Join(v[1])
            v[2] = @_Join(v[2])
            @score = @score + v[3]

    _Extract: =>
        @_cached = true
        miniz.extract_all(@_src, @_dest)
        @_PraseWdat(join(@_dest, "wdat.lua"))

    Clean: => @_Gc()
